//引入axios
import axios from 'axios';
import qs from 'qs';

//获取token
let info =  localStorage.getItem("blog_users") ? JSON.parse(localStorage.getItem("blog_users")):null;
let token = info? info.token :null;

//自定义post请求时候数据类型,默认是json
let headers = {
  // 'Content-Type': 'application/x-www-form-urlencoded'
};

if(token){
   console.log("已经登录过了，获取到了token")
   headers['Authorization'] = token; //设置默认的Authorization头
}

// 创建一个axios实例(对象)
const axiosInstance = axios.create({
  //配置选项
  //baseURL: 'https://api.example.com', // 你的API基础路径
  headers: headers
});

// axiosInstance.defaults.headers.common['Authorization'] = token;
// axiosInstance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

// 使用axiosInstance发送POST请求
export const post = (url, params) => {
  return axiosInstance.post(url, qs.stringify(params), {
    headers: headers
  })
  .then(res => res) // 确保返回的是响应数据
  .catch(err => err);
};

export default axiosInstance;
